/**
 * Created by hipo on 15-12-3.
 */
$(function() {
    //试图捕获arttemplate的错误
    var consoleError = console.error;
    console.error = function(err){
        consoleError(err);

        //err有可能不是String 类型，所以需要先toString
        if(err.toString().indexOf("Template Error") !=-1){
            var msg = err;
            var path = window.location.href;
            var line = '';
            var col = '';
            var error = '';
            if (App.isLogEnable){
                log4JS.error("Template Error,path:"+window.location.href+",msg:"+msg);
            }else{
               $(window).trigger("error",[msg,path,line,col,error]);
            }
        }
    };

    if (!App.isLogEnable) {
        // 开启日志框的时候不上报
        window.onerror = function(msg, path, line, col, error){
            var data = {};
            //不一定所有浏览器都支持col参数
            col = col || (window.event && window.event.errorCharacter) || 0;

            data.url = getShortPath(location.href);
            data.path = getShortPath(path);
            data.line = line + "";
            data.col = col + "";
            if (!!error && !!error.stack) {
                //如果浏览器有堆栈信息
                //直接使用
                data.msg = error.stack.toString();
            } else {
                data.msg = msg;
            }
            pushErrorLog(data);
            return false;
        };
    }


    var errorDataArray = [];
    var lastPostTime = 0; // 上次发送时间

    function pushErrorLog(data) {
        var hasSame = false;
        for (var i = 0; i < errorDataArray.length; i++) {
            var d = errorDataArray[i];
            if (data.url == d.url &&
                data.line == d.line &&
                data.col == d.col &&
                data.path == d.path) {
                d.count = d.count + 1;
                hasSame = true;
                return;
            }
        }
        if (!hasSame) {
            data.count = 1;
            errorDataArray.push(data);
        }
        if (Date.now() - lastPostTime > 60 * 1000) {
            postErrorMsg();
        }
    }

    function postErrorMsg() {
        // 每分钟发送一次
        if (errorDataArray.length) {
            var data = {};
            data.sessionId = App.sessionId;
            data.userAgent = navigator.userAgent;
            var logs = errorDataArray.slice(0);
            errorDataArray.length = 0;
            data.logs = logs;
            AppUtil.call(AppUtil.APIS.SEND_HTTP_REQUEST,
                {
                    type: "POST",
                    url: App.serverUrl + "/client/log/bh5.do",
                    data: JSON.stringify(data)
                }, function () {

                });
            lastPostTime = Date.now();

        }
    }

    setInterval(postErrorMsg, 60 * 1000);

    /**
     * 截取短路径，
     * @param path
     */
    function getShortPath(path) {
        if (!path) {
            return "";
        }
        if (path.indexOf("/js/") > 0 || path.indexOf("/html/") > 0 || path.indexOf("/base/") > 0 || path.indexOf("/lib/") > 0) {
            var pathArr = path.split("/");
            var rt = "";
            for (var i = 0; i < pathArr.length; i++) {
                if (pathArr[i] == "js" || pathArr[i] == "html" || pathArr[i] == "base" || pathArr[i] == "lib") {
                    rt = pathArr[i - 1] + "/" + pathArr[i];
                } else if (rt != "") {
                    rt = rt + "/" + pathArr[i];
                }
            }
            return rt;
        }
        return path;
    }
});



